package org.tbjd.client.decorators;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.tbjd.client.beans.Project;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.ProjectConstants;
import org.tbjd.client.guis.AbstractItem;
import org.tbjd.client.images.projectSwitcher.grid.GridImageBundle;
import org.tbjd.client.interfaces.ProjectDecorator;
import org.tbjd.client.listeners.projectSwitcher.ProjectRowListener;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;

public class ProjectsGridDecorator implements ProjectDecorator {

	private GridImageBundle gridImageBundle = (GridImageBundle) GWT.create(GridImageBundle.class);
	private ToggleButton starredToggle;
	private ToggleButton warningToggle;
	private Image image;
	private PushButton infoToggle;
	private PushButton deleteToggle;

	private static ProjectsGridDecorator instance = null;
	
	private ProjectsGridDecorator(){
	}

	public static ProjectsGridDecorator getInstance(){
		if(instance == null){
			instance = new ProjectsGridDecorator();
		}
		
		return instance;
	}

	/**
	 * This method decorates each project. This means; for each displayed attribute of a project, a widget is created containing the related
	 * information either as text or image.
	 * 
	 * @param List
	 *            <Project> projects
	 * @return List<Widget>
	 */

	public List<?> decorateOld(List<?> projects) {
		long begin = new Date().getTime();
		List<Widget> decoratedWidgets = new ArrayList<Widget>();

		for (Project project : (List<Project>) projects) {
			// starred button
			starredToggle = new ToggleButton(gridImageBundle.star_empty_16x16().createImage(), gridImageBundle.star_filled_16x16().createImage());
			starredToggle.setDown(project.isStarred());
			starredToggle.addClickListener(new ProjectRowListener());
			starredToggle.setStyleName("table_toggleButton");
			starredToggle.setTitle(project.getId().toString() + ":" + NotificationsConstants.STAR_CLICKED);

			decoratedWidgets.add(starredToggle);

			// priority button
			warningToggle = new ToggleButton(gridImageBundle.warning_empty_16x16().createImage(), gridImageBundle.warning_filled_16x16()
					.createImage());
			warningToggle.setDown(project.isPriority());
			warningToggle.addClickListener(new ProjectRowListener());
			warningToggle.setStyleName("table_toggleButton");
			warningToggle.setTitle(project.getId().toString() + ":" + NotificationsConstants.PRIORITY_CLICKED);

			decoratedWidgets.add(warningToggle);

			// name
			decoratedWidgets.add(new Label(project.getName()));

			// progress
			image = gridImageBundle.projectProgressBar_100x16().createImage();
			image.setWidth(project.getProgress().toString() + "px");
			decoratedWidgets.add(image);

			// begin date
			decoratedWidgets.add(new Label(new String(project.getBeginDate().toString()).substring(3, 10)));

			// end date
			decoratedWidgets.add(new Label(new String(project.getEndDate().toString()).substring(3, 10)));

			// info
			infoToggle = new PushButton(gridImageBundle.info_empty_16x16().createImage(), gridImageBundle.info_filled_16x16().createImage());
			infoToggle.setStyleName("table_toggleButton");
			infoToggle.addClickListener(new ProjectRowListener());
			infoToggle.setTitle(project.getId().toString() + ":" + NotificationsConstants.INFO_CLICKED);
			decoratedWidgets.add(infoToggle);

			// status
			if (project.getStatus().equalsIgnoreCase(ProjectConstants.PROJECT_FINISHED_STATUS)) {
				decoratedWidgets.add(gridImageBundle.finished_16x16().createImage());
			}
			if (project.getStatus().equalsIgnoreCase(ProjectConstants.PROJECT_PAUSED_STATUS)) {
				decoratedWidgets.add(gridImageBundle.paused_16x16().createImage());
			}
			if (project.getStatus().equalsIgnoreCase(ProjectConstants.PROJECT_RUNNING_STATUS)) {
				decoratedWidgets.add(gridImageBundle.running_16x16().createImage());
			}
			if (project.getStatus().equalsIgnoreCase(ProjectConstants.PROJECT_STOPPED_STATUS)) {
				decoratedWidgets.add(gridImageBundle.stopped_16x16().createImage());
			}

			// delete
			deleteToggle = new PushButton(gridImageBundle.delete_empty_16x16().createImage(), gridImageBundle.delete_filled_16x16().createImage());
			deleteToggle.setStyleName("table_toggleButton");
			deleteToggle.addClickListener(new ProjectRowListener());
			deleteToggle.setTitle(project.getId().toString() + ":" + NotificationsConstants.DELETE_CLICKED);
			decoratedWidgets.add(deleteToggle);

		}
		System.out.println("ProjectsGridDecorator.decorate() " + (Long) (new Date().getTime() - begin) + "[ms]");
		return decoratedWidgets;
	}

	public List<AbstractItem> decorate(List<Project> projects) {
		// TODO Auto-generated method stub
		return null;
	}

}
